<?php
namespace Pay\Controller;
use Common\Controller\AdminbaseController;
class SettingController extends AdminbaseController{
	
	protected $options_model;
	
	function _initialize() {
		parent::_initialize();
		$this->options_model = D("Common/SystemOptions");
	}
	
	function alipay(){
	    C(S('sp_dynamic_config'));
		$option=$this->options_model->where("option_name='site_options'")->find();
		$cmf_settings=$this->options_model->where("option_name='cmf_settings'")->getField("option_value");
		$tpls=sp_scan_dir(C("SP_TMPL_PATH")."*",GLOB_ONLYDIR);
		$noneed=array(".","..",".svn");
		$tpls=array_diff($tpls, $noneed);
		$this->assign("templates",$tpls);
	    $alipayconfig=array(
            seller_email=>C('seller_email'),
            notify_url=>C('notify_url'),
            return_url=>C('return_url'),
            successpage=>C('successpage'),
            errorpage=>C('errorpage'),
            partner=>C('partner'),
            key=>C('key'),
	    );

		$adminstyles=sp_scan_dir("public/simpleboot/themes/*",GLOB_ONLYDIR);
		$adminstyles=array_diff($adminstyles, $noneed);
		$this->assign("adminstyles",$adminstyles);
		if($option){
			$this->assign((array)json_decode($option['option_value']));
			$this->assign("option_id",$option['option_id']);
		}
		$this->assign("alipayconfig",$alipayconfig);
		$this->assign("cmf_settings",json_decode($cmf_settings,true));
		$this->display("alipay");
	}
	
	function site_post(){
		if (IS_POST) {
			if(isset($_POST['option_id'])){
				$data['option_id']=intval($_POST['option_id']);
			}

			// 支付宝支付的配置参数
			$configs["seller_email"]=$_POST['options']['seller_email'];
			$configs["notify_url"]=$_POST['options']['notify_url'];
			$configs["return_url"]=$_POST['options']['return_url'];	
			$configs["successpage"]=$_POST['options']['successpage'];	
			$configs["errorpage"]=$_POST['options']['errorpage'];	
			$configs["partner"]=$_POST['options']['partner'];	
			$configs["key"]=$_POST['options']['key'];

			sp_set_dynamic_config($configs);
				
			$data['option_name']="site_options";
			$data['option_value']=json_encode($_POST['options']);
			if($this->options_model->where("option_name='site_options'")->find()){
				$r=$this->options_model->where("option_name='site_options'")->save($data);
			}else{
				$r=$this->options_model->add($data);
			}
			
			$banned_usernames=preg_replace("/[^0-9A-Za-z_\x{4e00}-\x{9fa5}-]/u", ",", $_POST['cmf_settings']['banned_usernames']);
			$_POST['cmf_settings']['banned_usernames']=$banned_usernames;

			sp_set_cmf_setting($_POST['cmf_settings']);
			if ($r!==false) {
				$this->success("保存成功！");
			} else {
				$this->error("保存失败！");
			}
			
		}
	}


	
}